#delimit;
clear;
set more off;
capture log close;
set more off;
/*****
CHANGE PATH
*****/;
/*This program creates the variables for sectors attended. For this, I need info fron NSC, NSLDS loans and NSLDS Pell Grants*/
local pathtab "/data";
local out "/data";

use "`out'/dataset_for_regs_new_RandR_noCB.dta", clear;
keep pid first_col_state_earl_new;

merge 1:m pid using "`pathtab'/sector_tocreate_ever_state.dta";


sort pid;
forvalues yo=22(1)41 {; //this means we are going to have values for 21 to 34
gen sector_t_`yo'=0;
replace sector_t_`yo'=sector_ipeds if (((enroll_beg_pell-dob)/365<`yo' | (per_beg_dt-dob)/365<`yo' | (enroll_beg-dob)/365<`yo')) & (first_col_state_earl_new==state_ipeds | first_col_state_earl_new==state_nsc);
gen with_pub4_`yo'=0;
replace with_pub4_`yo'=1 if sector_t_`yo'==1;
gen with_pub2_`yo'=0;
replace with_pub2_`yo'=1 if sector_t_`yo'==4 | sector_t_`yo'==7;
gen with_priv_nfp4_`yo'=0;
replace with_priv_nfp4_`yo'=1 if sector_t_`yo'==2;
gen with_priv_nfp2_`yo'=0;
replace with_priv_nfp2_`yo'=1 if sector_t_`yo'==5 | sector_t_`yo'==8;
gen with_priv_fp_`yo'=0;
replace with_priv_fp_`yo'=1 if sector_t_`yo'==3 | sector_t_`yo'==6 | sector_t_`yo'==9;

by pid: egen ever_with_pub4_`yo'=max(with_pub4_`yo');
by pid: egen ever_with_pub2_`yo'=max(with_pub2_`yo');
by pid: egen ever_with_priv_nfp4_`yo'=max(with_priv_nfp4_`yo');
by pid: egen ever_with_priv_nfp2_`yo'=max(with_priv_nfp2_`yo');
by pid: egen ever_with_priv_fp_`yo'=max(with_priv_fp_`yo');

drop sector_t_`yo' with_pub4_`yo' with_pub2_`yo' with_priv_nfp4_`yo' with_priv_nfp2_`yo' with_priv_fp_`yo';
local yo_1=`yo'-1;
rename ever_with_pub4_`yo' ever_with_pub4_`yo_1'_home;
rename ever_with_pub2_`yo' ever_with_pub2_`yo_1'_home;
rename ever_with_priv_nfp4_`yo' ever_with_priv_nfp4_`yo_1'_home;
rename ever_with_priv_nfp2_`yo' ever_with_priv_nfp2_`yo_1'_home;
rename ever_with_priv_fp_`yo' ever_with_priv_fp_`yo_1'_home;
};
by pid: keep if _n==1;
keep pid ever_with*;
sort pid;
save "`pathtab'/sector_homestate_noCB.dta", replace;

